/**
 * 
 */
package com.game.jumpdino.logic.visual;

import com.game.jumpdino.logic.Playground;

import android.graphics.Point;

/**
 * @author HugoGu
 *
 */
public class Cloud extends Holder {
	private int flyingHeight = 2400;
	private int flyedHeight = 0;
	private Point hookPoint = new Point(-5, 80);
	private Playground playground;
	
	public Cloud(){
		//holderPosition = new Point(15, 65);
	}
	
	@Override
	public void onHit(VisualBase visual, Playground ground){
		// Only affect once and must jump on it.
		if ((flyedHeight < flyingHeight) && (visual.getVelocity().y > 0) && (!ground.avatar.isHasTool())){
			this.playground = ground;
			playSound(ground.getContext());
			this.setVy(-40);
			visual.setVx(0.0f);
			visual.setVy(-12);
			float offsetX = getPosition().left - visual.getPosition().left;
			float offsetY = getPosition().top - visual.getPosition().top;
			this.move(hookPoint.x - offsetX, hookPoint.y - offsetY);
			ground.avatar.setHasTool(true);
			visual.follow(this);
		}
	}
	
	//Override
	public void onMove(float offsetX, float offsetY){
		if (flyedHeight > flyingHeight){
			while(getFollows().size() > 0){
				// The unfollow method will remove the follow;
				getFollows().get(0).unFollow(this);
			}
			if (playground != null){
				playground.avatar.setHasTool(false);
			}
		} else if (getFollows().size() > 0 && offsetY < 0){
			flyedHeight += Math.max(Math.abs(offsetY), Math.abs(getVelocity().y));
		}
	}
}
